﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PersonalWebSite.Common.Google.DTO
{
    /// <summary>
    /// Defines a Google Activity Item
    /// </summary>
    public class ActivityItem
    {
        /// <summary>
        /// Gets or Sets the Id
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// Gets or Sets the Url
        /// </summary>
        public string Url { get; set; }

        /// <summary>
        /// Gets or Sets the Title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// Gets or Sets the Publish Date
        /// </summary>
        public DateTime DatePublished { get; set; }

        /// <summary>
        /// Gets or Sets the Updated Date
        /// </summary>
        public DateTime DateUpdated { get; set; }

        /// <summary>
        /// Gets or Sets the Activity Object
        /// </summary>
        public ActivityObject Object { get; set; }

        /// <summary>
        /// Gets or Sets the Annotation
        /// </summary>
        public string Annotation { get; set; }

        /// <summary>
        /// Gets or Sets the Geocode
        /// </summary>
        public Geocode Geocode { get; set; }

        /// <summary>
        /// Gets or Sets the Address
        /// </summary>
        public string Address { get; set; }

        /// <summary>
        /// Gets or Sets the Place Id
        /// </summary>
        public string PlaceId { get; set; }

        /// <summary>
        /// Gets or Sets the Place Name
        /// </summary>
        public string PlaceName { get; set; }

        /// <summary>
        /// Gets or Sets the ETag
        /// </summary>
        public string ETag { get; set; }

        #region Computed Properties
        /// <summary>
        /// Gets the computed subtitle
        /// </summary>
        public string Subtitle
        {
            get
            {
                return DatePublished.ToString("HH:mm:ss") +
                    (Object == null || Object.Attachements == null || Object.Attachements.Length == 0 ? string.Empty :
                    " " + string.Join(", ", Object.Attachements.Where(a => !string.IsNullOrWhiteSpace(a.DisplayName)).Select(a => HttpUtility.HtmlDecode(a.DisplayName))));
            }
        }

        /// <summary>
        /// Gets the computed image for this activity
        /// </summary>
        public string ImageUrl
        {
            get
            {
                return (Object == null || Object.Attachements == null || Object.Attachements.Length == 0 ? null :
                    Object.Attachements.Where(a => a.AttachementObjectType == AttachementObjectType.Photo).Select(a => a.FullImage.Url).FirstOrDefault());
            }
        }

        /// <summary>
        /// Gets the computed embed object url for this activity
        /// </summary>
        public string EmbedUrl
        {
            get
            {
                return (Object == null || Object.Attachements == null || Object.Attachements.Length == 0 ? null :
                    Object.Attachements.Where(a => a.AttachementObjectType == AttachementObjectType.Video).Select(a => a.Embed.Url).FirstOrDefault());
            }
        }

        /// <summary>
        /// Gets the computed embed object url image for this activity
        /// </summary>
        public string EmbedImageCssUrl
        {
            get
            {
                return (Object == null || Object.Attachements == null || Object.Attachements.Length == 0 ? null :
                    Object.Attachements.Where(a => a.AttachementObjectType == AttachementObjectType.Video).Select(a => string.Format("url('{0}')", a.Image.Url)).FirstOrDefault());
            }
        }

        /// <summary>
        /// Gets the computed google small map image CSS url for this activity
        /// </summary>
        public string GoogleMiniMapImageCssUrl
        {
            get
            {
                return Geocode == null ? null : string.Format("url('http://maps.googleapis.com/maps/api/staticmap?center={0},{1}&zoom=1&size=100x50&markers=size:tiny%7Ccolor:0x125C8A%7C{0},{1}&sensor=false')", Geocode.Latitude, Geocode.Longitude);
            }
        }
        #endregion

        #region Helper Classes
        /// <summary>
        /// Defines an Activity Object
        /// </summary>
        public class ActivityObject
        {
            /// <summary>
            /// Gets or Sets the Activity Object Type
            /// </summary>
            public ActivityObjectType Type { get; set; }

            /// <summary>
            /// Gets or Sets the Id
            /// </summary>
            public string Id { get; set; }

            /// <summary>
            /// Gets or Sets the Content
            /// </summary>
            public string Content { get; set; }

            /// <summary>
            /// Gets or Sets the Original Content
            /// </summary>
            public string OriginalContent { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// Gets or Sets the Attachements
            /// </summary>
            public Attachement[] Attachements { get; set; }
        }

        /// <summary>
        /// Defines an Activity Attachement
        /// </summary>
        public class Attachement
        {
            /// <summary>
            /// Gets or Sets the Attachement Object Type
            /// </summary>
            public AttachementObjectType AttachementObjectType { get; set; }

            /// <summary>
            /// Gets or Sets the Id
            /// </summary>
            public string Id { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// Gets or Sets the Display Name
            /// </summary>
            public string DisplayName { get; set; }

            /// <summary>
            /// Gets or Sets the Content
            /// </summary>
            public string Content { get; set; }

            /// <summary>
            /// Gets or Sets the Image
            /// </summary>
            public Image Image { get; set; }

            /// <summary>
            /// Gets or Sets the Full Image
            /// </summary>
            public Image FullImage { get; set; }

            /// <summary>
            /// Gets or Sets the Embed Video Object
            /// </summary>
            public Embed Embed { get; set; }
        }

        /// <summary>
        /// Defines an Activity Image Object
        /// </summary>
        public class Image
        {
            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Type { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public int Width { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public int Height { get; set; }
        }

        /// <summary>
        /// Defines an Activity Embed Object
        /// </summary>
        public class Embed
        {
            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Url { get; set; }

            /// <summary>
            /// Gets or Sets the Url
            /// </summary>
            public string Type { get; set; }
        }

        /// <summary>
        /// Defines Activity Object Types
        /// </summary>
        public enum ActivityObjectType
        {
            Note = 0,
            Activity = 1
        }

        /// <summary>
        /// Defines Attachement Object Types
        /// </summary>
        public enum AttachementObjectType
        {
            Photo = 0,
            Video = 1,
            Article = 2,
            Album = 3,
            PhotoAlbum = 3
        }
        #endregion
    }
}